<template>
  <div class="app-container">
    <el-page-header @back="backUp" class="backUp" :content="id?'修改奖励规则':'添加奖励规则'"></el-page-header>
    <el-form :model="formFieldsData" :rules="rules" ref="ruleForm" label-position="left" label-width="200px">
      <el-form-item label="奖励规则名称：" prop='name'>
        <el-input v-model="formFieldsData.name" type='text' style="width: 400px" autocomplete="off" clearable placeholder='请输入规则名称'/>
      </el-form-item>
      <el-form-item label="奖励对象：" prop='obj_type'>
        <el-select v-model="formFieldsData.obj_type" placeholder="请选择奖励对象" style="width: 400px">
          <el-option
            v-for="item in optionsType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户动作：">
        邀请新用户
      </el-form-item>
      <el-form-item label="添加奖励类型：" >
        <el-button size='medium' type='primary' @click="dialogFormVisible=true">添加</el-button>
        <el-table :data="rewardList" border style="width: 400px;margin-top:20px" size='mini' v-if="rewardList.length>0">
          <el-table-column prop="actionName" label="被邀请者完成指定行为" align='center'></el-table-column>
          <el-table-column prop="money" label="奖励金额" width='80px' align='center'></el-table-column>
          <el-table-column label="操作" width='60px' align='center'>
            <template slot-scope="item">
              <el-button type="text" size='mini' @click="deleteReward(item.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="是否需要用户手动领取：">
        <el-radio-group v-model="formFieldsData.is_receive">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="起始时间：" prop='start_time'>
        <el-date-picker
          v-model="time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="filter-item form-search-input1"
          @change='timeChange'
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否启用：">
        <el-radio-group v-model="formFieldsData.enable">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="backUp" size='medium'>取 消</el-button>
        <el-button type="primary" @click="handleSure"  size='medium'>确 定</el-button>
      </el-form-item>
    </el-form>
    <!-- 添加奖励类型 -->
    <el-dialog :visible.sync="dialogFormVisible" width='516px' :show-close='false'>
      <div slot="title" class="dialog-title">添加奖励类型</div>
      <el-form :model="form" label-width='170px' :label-position="labelPosition">
        <el-form-item label="被邀请者完成指定行为：">
          <el-select v-model="form.type" placeholder="请选择指定行为" style="width:100%" @change="actionChange">
            <el-option :label="item.label" :value="item.value" v-for="item in actionList" :key='item.value'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="奖励金额（元）：">
          <el-input v-model="form.money" type='number' autocomplete="off" placeholder='请输入奖励金额'></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size='medium'>取 消</el-button>
        <el-button type="primary" @click="sure" size='medium'>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {parseTime} from '@/utils/index'
import formOperate from '@/layout/mixin/formOperate'
export default {
  name: 'Index',
  mixins: [formOperate],
  data() {
    return {
      upurl:'/promotion/admin/inviteNew',
      formName:'redRule',
      optionsType:[
        {label:'个人',value:1},
        {label:'企业',value:2}
      ],
      formFieldsData:{
        name:'',
        obj_type:'',
        is_receive:0,
        enable:1,
        start_time:'',
        end_time:'',
        complete_type:''
      },
      rules:{
        name:[
          { required: true, message: '请输入奖励规则名称', trigger: 'blur' }
        ],
        obj_type:[
          { required: true, message: '请选择奖励对象', trigger: 'change' }
        ],
        start_time:[
          { required: true, message: '请选择时间', trigger: 'blur' }
        ]
      },
      rewardList:[],// 奖励类型
      time:'',
      dialogFormVisible:false,
      actionList:[
        {label:'注册',value:'1'},
        {label:'发布简历',value:'2'},
        {label:'企业认证',value:'3'},
        {label:'发布职位',value:'4'}
      ],
      form:{
        type:'',
        actionName:'',
        money:''
      },
      id:''
    }
  },
  mounted() {
    this.id = this.$route.query.id || ''
    if (this.id) {
      this.getInviteInfo()
    }
  },
  methods: {
    getInviteInfo() {
      this.$http.get(this.upurl + '/' + this.id).then((response) => {
        console.log(response)
        let info = response.data
        this.formFieldsData.name = info.name
        this.formFieldsData.obj_type = info.obj_type
        this.formFieldsData.is_receive = info.is_receive
        this.formFieldsData.enable = info.enable
        this.formFieldsData.start_time = info.start_time
        this.formFieldsData.end_time = info.end_time
        this.time = [parseTime(this.formFieldsData.start_time),parseTime(this.formFieldsData.end_time)]
        this.rewardList = []
        info.type_data.forEach(res=>{
          this.rewardList.push({
            type:res.type,
            money:res.money,
            actionName:this.getType(res.type)
          })
        })
      })
    },
    // 获取奖励类型
    getType(type) {
      let idx = this.actionList.findIndex(res=>{
        return res.value == type
      })
      return this.actionList[idx].label
    },
    // 监听时间选择
    timeChange(t) {
      this.formFieldsData.start_time = new Date(t[0]).getTime() / 1000
      this.formFieldsData.end_time = new Date(t[1]).getTime() / 1000
    },
    // 添加奖励类型
    sure() {
      for (let key in this.form) {
        if (this.form[key] == '') return
      }
      let val = {...this.form}
      let idx = this.rewardList.findIndex(res=>{
        return res.type == val.type
      })
      if (idx > -1) {
        this.$message('当前行为已添加')
        return
      }
      this.rewardList.push(val)
      for (let key in this.form) {
        this.form[key] = ''
      }
      this.dialogFormVisible = false
    },
    // 奖励类型选择后
    actionChange(e) {
      let idx = this.actionList.findIndex(res=>{
        return res.value == e
      })
      this.form.actionName = this.actionList[idx].label
    },
    // 删除奖励类型
    deleteReward(idx) {
      this.rewardList.splice(idx,1)
    },
    // 确定
    handleSure() {
      if (this.rewardList.length == 0) {
        this.$message.error('奖励类型不能为空')
        return
      }
      let arr = []
      this.rewardList.forEach(res=>{
        arr.push({type:res.type,money:res.money})
      })
      this.formFieldsData.complete_type = JSON.stringify(arr)
      this.formFieldsData['request_type'] = 1
      console.log(this.formFieldsData)
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.id) {
            this.$http.put(this.upurl + '/' + this.id, this.formFieldsData).then(response => {
              this.backUp()
            })
          } else {
            this.$http.post(this.upurl, this.formFieldsData).then(response => {
              this.backUp()
            })
          }
        } else {
          return false
        }
      })
    },
    //返回
    backUp() {
      this.$router.go(-1)
    },
  }
}
</script>
<style>
  .details .el-dialog__body{
    height: 700px;
    overflow-y: auto;
  }
</style>
<style lang="scss" scoped>

  .title{
    font-size: 16px;
    margin-bottom:10px;
  }
  .info{
    line-height: 30px;
  }
  .img_box{
    text-align: center;
  }
  .job_list{
    border:1px solid #eee;
    padding: 10px;
    border-radius: 4px;
  }
  .backUp{
    margin-bottom: 30px;
  }
  .tips{
    font-size: 14px;
    color:#999;
    margin-bottom: 10px;
  }
  .form_table{
    margin-bottom: 20px;
  }
  .label{
    width: 140px;
    height: 50px;
    line-height: 50px;
    font-size: 14px;
    color:#777;
    padding-left: 4px;
    border:1px solid #eee;
    margin:0 -1px -1px 0;
  }
  .el_form_item{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
    margin:0;
    padding:0 10px;
    border:1px solid #eee;
    margin:0 -1px -1px 0;
  }
  .ipt{
    width: 80px;
  }
  .el-icon-circle-plus-outline{
    width: 100px;
    height: 147px;
    line-height: 147px;
    text-align: center;
    border:1px solid #eee;
    box-sizing: border-box;
    font-size: 36px;
    color:#777;
    cursor: pointer;
  }
</style>